//
//  MovieCommentStartView.m
//  PeopleStreet
//
//  Created by 辰领科技 on 16/3/25.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "MovieCommentStartView.h"

@interface MovieCommentStartView ()
@property(nonatomic,strong)void(^complect)(CGFloat index);
@property(nonatomic,assign)NSInteger count;
@end

@implementation MovieCommentStartView

- (instancetype)initWithFrame:(CGRect)frame NumberOfStars:(NSInteger )numberOfStars complect:(void(^)(CGFloat index))complect{
    self = [super initWithFrame:frame];
    _complect = complect;
    _count = numberOfStars;
    for (NSInteger i = 0; i<numberOfStars; i++) {
        UIButton * stars = [UIButton buttonWithType:UIButtonTypeCustom];
        stars.frame = flexibleFrame(CGRectMake(47*i, 0, 30, 28), NO);
        [stars setBackgroundImage:[UIImage imageNamed:@"movieComment1"] forState:UIControlStateNormal];
        [stars setBackgroundImage:[UIImage imageNamed:@"movieComment3"] forState:UIControlStateSelected];
        stars.selected = YES;
        stars.tag = 100+i;
        [stars addTarget:self action:@selector(changeStars:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:stars];
        
        
        
    }
    
    return self;
}

- (void)changeStars:(UIButton*)sender{
    [sender setBackgroundImage:[UIImage imageNamed:@"movieComment2"] forState:UIControlStateNormal];
    [sender setBackgroundImage:[UIImage imageNamed:@"movieComment3"] forState:UIControlStateSelected];
    sender.selected = !sender.selected;
    
    for (NSInteger i = 100; i<100+_count; i++) {
        
        UIButton * button = (UIButton *)[self viewWithTag:i];
        if (i<sender.tag) {
            [button setBackgroundImage:[UIImage imageNamed:@"movieComment1"] forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageNamed:@"movieComment3"] forState:UIControlStateSelected];
            button.selected = YES;
        }else if (i>sender.tag){
            [button setBackgroundImage:[UIImage imageNamed:@"movieComment1"] forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageNamed:@"movieComment3"] forState:UIControlStateSelected];
            button.selected = NO;
        }else{
            
        }
        
    }
    
    if (sender.selected) {
        _complect(sender.tag - 99);
    }else{
        _complect(sender.tag - 99.5);
    }
}

@end
